<?php
/**
 * Created by PhpStorm.
 * Login: 曹旺
 * Date: 2018-03-06
 * Time: 10:55
 */

namespace app\index\controller;

use app\index\model\CommodityClass;

class Commodity extends Base
{
    /**
     * @return mixed|string
     * 商品分类
     * @author  曹旺
     * @data  14:55
     */
    public function commodityclass()
    {
        //接受秘钥
        $key = input('post.sign');
        $result='';

        if (isset($key) && $key!=''){
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/OrderCommodity/commodityclass";
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            //dump($res_pass);die;
            if ($res_pass) {
                //实例化商品分类model
                $res = new CommodityClass();
                $result = $res->commodity_class();
                if (!empty($result)){
                    $data=['code'=>200,"msg"=>'请求成功','content'=>$result];
                }else{
                    $data=['code'=>-1,'msg'=>'请求成功，但无相关数据','content'=>$result];
                }
            }else{
                $data=['code'=>199,'msg'=>"请求失败，未通过验证",'content'=>$result];
            }
        }else{
            $data=['code'=>199,'msg'=>'请求方式失败','content'=>$result];
        }

        //dump($data);die;
        return $data;
    }

    /**
     * @return mixed|string
     *商品详情
     * @author  曹旺
     * @data  15:40
     */
    public function commodity_message()
    {
        $result = '';
        $user_discount_rate=1;
        $id = input('post.id');
        $user_id = input('post.user_id');
        $key = input('post.sign');
        if (isset($user_id)){
            $user_discount_rate=$this->user_discount_rate($user_id);
        }
        if (isset($id) && isset($key)) {
            //return 123123;
            //接受秘钥
            $value='http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Commodity/commodity_message';
            //$value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Commodity/commodity_list";
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            if ($res_pass) {
                //实例化商品分类model
                $res = new CommodityClass();
                $result = $res->commodity_info($id,$user_discount_rate);
                if ($result!=''){
                    $data=['code'=>200,'msg'=>"请求成功",'content'=>$result];
                }else{
                    $data=['code'=>-1,'msg'=>"请求成功,但无相关数据",'content'=>$result];
                }
            }else{
                $data=['code'=>199,'msg'=>"请求失败，未通过验证",'content'=>$result];
            }
        }else{
            $data=['code'=>199,'msg'=>"请求失败",'content'=>$result];
        }
        //dump($data);die;
        return $data;
    }

    /**
     * @return mixed|string
     * 商品首页
     * @author  曹旺
     * @data  16:59
     */
    public function index()
    {
        //$result_json = '';
        $page = input('post.page');
        $limit = input('post.limit');
        //页码不存在默认为1
        if (empty($page)) {
            $page = 1;
        }
        //每页条数不存在默认为5
        if (empty($limit)) {
            $limit = 5;
        }
        $key = input('post.sign');
        //dump($key);die;
        if (isset($key) && $key!='') {
            //接受秘钥
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Commodity/index";
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            if ($res_pass) {
                $res = new CommodityClass();
                $result = $res->commodity($page, $limit);
                if (!empty($result)){
                    $result_json=['code'=>200,'msg'=>"请求成功",'content'=>$result];
                }else{
                    $result_json=['code'=>-1,'msg'=>"请求成功，但无相关数据",'content'=>''];
                }
            }else{
                $result_json=['code'=>199,'msg'=>"非法入侵",'content'=>''];
            }
        }else{
            $result_json=['code'=>199,'msg'=>"请求方式错误",'content'=>''];
        }
        return $result_json;
    }

    /**
     * 商品列表
     * @author  曹旺
     * @data  16:59
     */
    public function commodity_list(){
        $page = input('post.page');
        $limit = input('post.limit');
        $user_id=input('post.user_id');
        $user_discount_rate=1;
        //页码不存在默认为1
        if (isset($user_id)){
            $user_discount_rate=$this->user_discount_rate($user_id);
        }
        if (empty($page)) {
            $page = 1;
        }
        //每页条数不存在默认为5
        if (empty($limit)) {
            $limit = 5;
        }
        $key = input('post.sign');
        if (isset($key) && $key!='') {
            //接受秘钥
            $value = "http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Commodity/commodity_list";
            //验证秘钥
            $res_pass = $this->encryption($key, $value);
            if ($res_pass) {
                $res = new CommodityClass();
                $result = $res->commodityList($page, $limit,$user_discount_rate);
                if (!empty($result)){
                    $result_json=['code'=>200,'msg'=>"请求成功",'content'=>$result];
                }else{
                    $result_json=['code'=>-1,'msg'=>"请求成功，但无相关数据",'content'=>''];
                }
            }else{
                $result_json=['code'=>199,'msg'=>"非法入侵",'content'=>$key];
            }
        }else{
            $result_json=['code'=>199,'msg'=>"请求方式错误",'content'=>''];
        }
        return $result_json;
    }

    /**
     * @return mixed|string【商品id数组】
     * 虚拟购物车
     * @author  曹旺
     * @data  15:42
     */
    public function uncommodity_car(){
        //return 'sdf1';
        if ($this->request->isPost()){
            $key=input('post.sign');
            $id=input('post.id/a');
            $user_discount_rate=1;
            $user_id=input('post.user_id');
            if (isset($user_id)){
                $user_discount_rate=$this->user_discount_rate($user_id);
            }
            $value='http://www.importndsf.com/import_nbsm/nbsm_shopping/public/index.php/index/Commodity/uncommodity_car';
            $sign_pass=$this->encryption($key,$value);
            if (!$sign_pass){
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
                return $result_json;
            }
            /*$token_pass=$this->encryption_token($token);
            if (!$token_pass){
                $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
                return $result_json;
            }*/
            $model=new CommodityClass();
            $commodity_car=$model->unset_car($id,$user_discount_rate);
            if (!empty($commodity_car)){
                $result_json = ['code' => 200, 'msg' => '请求成功', 'content' => $commodity_car];
                return $result_json;
            }else{
                $result_json = ['code' => -1, 'msg' => '无数据', 'content' => ''];
                return $result_json;
            }

        }else{
            $result_json = ['code' => 199, 'msg' => '非法入侵', 'content' => ''];
            return $result_json;
        }
    }

}